package com.shujia.scala

import scala.io.Source

object Demo21ClazzNum {
  def main(args: Array[String]): Unit = {
    /**
     * 1、统计班级人数
     */

    //1、读取数据
    val studentsList: List[String] = Source.fromFile("data/students.txt").getLines().toList

    //2、取出班级
    val clazzList: List[String] = studentsList.map((student: String) => {
      //取出班级
      val clazz: String = student.split(",").last
      //返回
      clazz
    })

    //3、按照班级分组
    val groupByList: Map[String, List[String]] = clazzList.groupBy(clazz => clazz)

    //4、统计班级的人数

    val clazzNUm: Map[String, Int] = groupByList.map((kv: (String, List[String])) => {
      val clazz: String = kv._1
      val clazzs: List[String] = kv._2
      //计算班级的人数
      val num: Int = clazzs.length
      (clazz, num)
    })

    clazzNUm.foreach(println)

  }

}
